import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect: '/home',  // 重定向到主页
    children: [
      { path: '403', name: 'Auth', meta: { name: '无权限' }, component: () => import('../views/manager/Auth') },
      { path: 'home', name: 'Home', meta: { name: '系统首页' }, component: () => import('../views/manager/Home') },
      { path: 'depmanage', name: 'depManage', meta: { name: '部门管理' }, component: () => import('../views/manager/depManage.vue') },
      { path: 'staffmanage', name: 'staffManage', meta: { name: '员工管理' }, component: () => import('../views/manager/staffManage.vue') },
      { path: 'orderinfo', name: 'orderInfo', meta: { name: '请假信息' }, component: () => import('../views/manager/orderInfo.vue') },
      { path: 'check', name: 'Check', meta: { name: '我的待办' }, component: () => import('../views/manager/Check.vue') },
      { path: 'charts', name: 'Charts', meta: { name: '数据统计' }, component: () => import('../views/manager/Charts.vue') }
    ]
  },
  { path: '/login', name: 'Login', meta: { name: '登录' }, component: () => import('../views/Login.vue') },
  { path: '*', name: '404', meta: { name: '无法访问' }, component: () => import('../views/404.vue') },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // to 是到达的路由信息
  // from 是开源的路由信息
  // next 是帮助我们跳转路由的函数
  let user = localStorage.getItem('user') || ''
  // console.log(user,'user')
  if (!user || user === {} ||user === null || user===undefined || user === '') {
    // 如果没有登录，并且是跳转至登录页
    if (to.path === '/login') {
      // 直接跳转
      next();
    } else {
      // 否则直接强制跳转至登录页
      router.push('/login').catch(err => {
        console.log(err)
      })
    }
  } else {
    // 如果已经登录，并且不是跳转至登录页
    if (to.path !== '/login') {
      let adminPaths=['/depmanage','/staffmanage','/check','/charts']
      let checkPaths =['/check','/charts']
      let exceptPaths =['/depmanage','/staffmanage']
      // 直接跳转
      if(JSON.parse(user).role !== '管理员' && JSON.parse(user).role !== '考勤员'&& JSON.parse(user).post === '员工'   && adminPaths.includes(to.path)){
        next('/404')
      }else if(JSON.parse(user).role !== '考勤员' && JSON.parse(user).role !== '管理员' && JSON.parse(user).post === '员工' && checkPaths.includes(to.path)){
        next('/404')
      }else if((JSON.parse(user).role === '考勤员' && exceptPaths.includes(to.path))||(JSON.parse(user).role === '普通用户' && JSON.parse(user).post !== '员工' && exceptPaths.includes(to.path))) {
        next('/404')
      }else{
        // console.log(111)
        next();
      }
    } else {
      // 否则直接强制跳转至上一个页面
      router.push(from.path)
    }
  }
})

export default router
